﻿using Android_Helper.entity;
using Android_Helper.Utils;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Android_Helper.golang
{
    class DescTableUtils
    {

        public static string GoLangCRUD(string connectiongString, string dbName, string tableName)
        {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.AppendLine(GolangStruct(connectiongString, dbName, tableName));
            stringBuilder.AppendLine(GolangGetColumns(connectiongString, dbName, tableName));
            stringBuilder.AppendLine(GolangRowToObject(connectiongString, dbName, tableName));
            stringBuilder.AppendLine(GolangRowsToObject(connectiongString, dbName, tableName));
            stringBuilder.AppendLine(GolangGetRow(connectiongString, dbName, tableName));
            stringBuilder.AppendLine(GolangGetRows(connectiongString, dbName, tableName));
            stringBuilder.AppendLine(GolangUpdateOrDelete(connectiongString, dbName, tableName));
            stringBuilder.AppendLine(GolangInsert(connectiongString, dbName, tableName));
            stringBuilder.AppendLine(GolangUpdateSQLAndParamsSegment(connectiongString, dbName, tableName));
            return stringBuilder.ToString();
        }
        public static string GolangStruct(string connectiongString, string dbName, string tableName)
        {
            List<TableColumnEntity> list = SchemaUtils.TableColumnToEntity(connectiongString, dbName, tableName);
            TableColumnEntity tableEntity = SchemaUtils.TableToEntity(connectiongString, dbName, tableName);
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.AppendLine("// " + NameUtils.LowercaseUnderlineToAllCapitalUpper(tableName) + " " + tableEntity.ColumnComment);
            stringBuilder.AppendLine("type " + NameUtils.LowercaseUnderlineToAllCapitalUpper(tableName) + " struct {");
            foreach (TableColumnEntity tableColumn in list)
            {
                stringBuilder.AppendLine(NameUtils.GoLangDialectID(NameUtils.LowercaseUnderlineToAllCapitalUpper(tableColumn.ColumnName)) + "  " + GolangTypeUtils.DbTypeToGolang(tableColumn.ColumnType) + "  // " + tableColumn.ColumnComment);
            }
            stringBuilder.AppendLine("}");
            stringBuilder.AppendLine();
            stringBuilder.AppendLine();
            stringBuilder.AppendLine("// " + NameUtils.LowercaseUnderlineToAllCapitalUpper(tableName) + "NULL " + tableEntity.ColumnComment);
            stringBuilder.AppendLine("type " + NameUtils.LowercaseUnderlineToAllCapitalUpper(tableName) + "NULL struct {");
            foreach (TableColumnEntity tableColumn in list)
            {
                stringBuilder.AppendLine(NameUtils.GoLangDialectID(NameUtils.LowercaseUnderlineToAllCapitalUpper(tableColumn.ColumnName)) + "  " + GolangTypeUtils.DbTypeToGolangNull(tableColumn.ColumnType) + "  // " + tableColumn.ColumnComment);
            }
            stringBuilder.AppendLine("}");
            return stringBuilder.ToString();
        }

        public static string GolangGetColumns(string connectiongString, string dbName, string tableName)
        {
            List<TableColumnEntity> list = SchemaUtils.TableColumnToEntity(connectiongString, dbName, tableName);
            TableColumnEntity tableEntity = SchemaUtils.TableToEntity(connectiongString, dbName, tableName);
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.AppendLine();
            stringBuilder.AppendLine("// GetColumns" + NameUtils.LowercaseUnderlineToAllCapitalUpper(tableName) + " string Find" + tableEntity.ColumnComment + " All Columns");
            stringBuilder.AppendLine("func GetColumns"+ NameUtils.LowercaseUnderlineToAllCapitalUpper(tableName) + "(" + NameUtils.LowercaseUnderlineToCammle(tableName) + " *" + NameUtils.LowercaseUnderlineToAllCapitalUpper(tableName) + ") string {");
            stringBuilder.Append("return \"");
            foreach (TableColumnEntity tableColumn in list)
            {
                if (list.IndexOf(tableColumn) < list.Count - 1)
                {
                    stringBuilder.Append("`" + tableColumn.ColumnName + "`, ");
                }
                else
                {
                    stringBuilder.Append("`" + tableColumn.ColumnName + "` ");
                }
            }
            stringBuilder.AppendLine("\"");
            stringBuilder.AppendLine("}");
            stringBuilder.AppendLine();
            return stringBuilder.ToString();
        }

        public static string GolangRowToObject(string connectiongString, string dbName, string tableName)
        {
            List<TableColumnEntity> list = SchemaUtils.TableColumnToEntity(connectiongString, dbName, tableName);
            TableColumnEntity tableEntity = SchemaUtils.TableToEntity(connectiongString, dbName, tableName);
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.AppendLine();
            stringBuilder.AppendLine("// RowToObject" + NameUtils.LowercaseUnderlineToAllCapitalUpper(tableName) + " err error " + tableEntity.ColumnComment + " ORM To Entity List");
            stringBuilder.AppendLine("func RowToObject" + NameUtils.LowercaseUnderlineToAllCapitalUpper(tableName) + "(query * sql.Row)(" + NameUtils.LowercaseUnderlineToCammle(tableName) + " *" + NameUtils.LowercaseUnderlineToAllCapitalUpper(tableName) + ", err error) {");
            stringBuilder.AppendLine("row := " + NameUtils.LowercaseUnderlineToAllCapitalUpper(tableName) + "NULL{}");
            stringBuilder.AppendLine("err = query.Scan(");
            string strtmp = string.Empty;
            for (int i = 0; i < list.Count; i++)
            {
                TableColumnEntity tableColumn = list[i];
                strtmp += "&row." + NameUtils.GoLangDialectID(NameUtils.LowercaseUnderlineToAllCapitalUpper(tableColumn.ColumnName)) + "  , ";
                if ((i > 0) && (i % 3 == 0))
                {
                    strtmp += "\n";
                }
            }
            //整除会多个换行
            switch (list.Count % 3)
            {
                case 0:
                    strtmp = strtmp.Remove(strtmp.Length - 3);
                    break;
                case 1:
                    strtmp = strtmp.Remove(strtmp.Length - 6);
                    break;
                case 2:
                    strtmp = strtmp.Remove(strtmp.Length - 2);
                    break;
                case 3:
                    strtmp = strtmp.Remove(strtmp.Length - 2);
                    break;
            }

            stringBuilder.AppendLine(strtmp + ")");
            stringBuilder.AppendLine("if err != nil && err != sql.ErrNoRows {");
            stringBuilder.AppendLine("  utils.Debug(\"QueryToObject\", err)");
            stringBuilder.AppendLine("  return");
            stringBuilder.AppendLine("}");
            stringBuilder.AppendLine();
            stringBuilder.AppendLine(NameUtils.LowercaseUnderlineToCammle(tableName) + "=&" + NameUtils.LowercaseUnderlineToAllCapitalUpper(tableName) + "{");
            strtmp = string.Empty;
            for (int i = 0; i < list.Count; i++)
            {
                TableColumnEntity tableColumn = list[i];
                strtmp += NameUtils.GoLangDialectID(NameUtils.LowercaseUnderlineToAllCapitalUpper(tableColumn.ColumnName)) + ": row." + NameUtils.GoLangDialectID(NameUtils.LowercaseUnderlineToAllCapitalUpper(tableColumn.ColumnName)) + "." + GolangTypeUtils.DbTypeNullToGo(GolangTypeUtils.DbTypeToGolangNull(tableColumn.ColumnType)) + "  , ";
                if ((i > 0) && (i % 3 == 0))
                {
                    strtmp += "\n";
                }
            }
            //整除会多个换行
            switch (list.Count % 3)
            {
                case 0:
                    strtmp = strtmp.Remove(strtmp.Length - 3);
                    break;
                case 1:
                    strtmp = strtmp.Remove(strtmp.Length - 6);
                    break;
                case 2:
                    strtmp = strtmp.Remove(strtmp.Length - 2);
                    break;
                case 3:
                    strtmp = strtmp.Remove(strtmp.Length - 2);
                    break;
            }

            stringBuilder.AppendLine(strtmp + "}");
            stringBuilder.AppendLine("return");
            stringBuilder.AppendLine("}");
            stringBuilder.AppendLine();
            return stringBuilder.ToString();
        }

        public static string GolangRowsToObject(string connectiongString, string dbName, string tableName)
        {
            List<TableColumnEntity> list = SchemaUtils.TableColumnToEntity(connectiongString, dbName, tableName);
            TableColumnEntity tableEntity = SchemaUtils.TableToEntity(connectiongString, dbName, tableName);
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.AppendLine();
            stringBuilder.AppendLine("// RowsToObject" + NameUtils.LowercaseUnderlineToAllCapitalUpper(tableName) + " err error " + tableEntity.ColumnComment + " ORM To Entity List");
            stringBuilder.AppendLine("func RowsToObject" + NameUtils.LowercaseUnderlineToAllCapitalUpper(tableName) + "(query * sql.Rows)(" + NameUtils.LowercaseUnderlineToCammle(tableName) + " *" + NameUtils.LowercaseUnderlineToAllCapitalUpper(tableName) + ", err error) {");
            stringBuilder.AppendLine("row := " + NameUtils.LowercaseUnderlineToAllCapitalUpper(tableName) + "NULL{}");
            stringBuilder.AppendLine("err = query.Scan(");
            string strtmp = string.Empty;
            for (int i = 0; i < list.Count; i++)
            {
                TableColumnEntity tableColumn = list[i];
                strtmp += "&row." + NameUtils.GoLangDialectID(NameUtils.LowercaseUnderlineToAllCapitalUpper(tableColumn.ColumnName)) + "  , ";
                if ((i > 0) && (i % 3 == 0))
                {
                    strtmp += "\n";
                }
            }
            //整除会多个换行
            switch (list.Count % 3)
            {
                case 0:
                    strtmp = strtmp.Remove(strtmp.Length - 3);
                    break;
                case 1:
                    strtmp = strtmp.Remove(strtmp.Length - 6);
                    break;
                case 2:
                    strtmp = strtmp.Remove(strtmp.Length - 2);
                    break;
                case 3:
                    strtmp = strtmp.Remove(strtmp.Length - 2);
                    break;
            }

            stringBuilder.AppendLine(strtmp + ")");
            stringBuilder.AppendLine("if err != nil && err != sql.ErrNoRows {");
            stringBuilder.AppendLine("  utils.Debug(\"QueryToObject\", err)");
            stringBuilder.AppendLine("  return");
            stringBuilder.AppendLine("}");
            stringBuilder.AppendLine();
            stringBuilder.AppendLine(NameUtils.LowercaseUnderlineToCammle(tableName) + "=&" + NameUtils.LowercaseUnderlineToAllCapitalUpper(tableName) + "{");
            strtmp = string.Empty;
            for (int i = 0; i < list.Count; i++)
            {
                TableColumnEntity tableColumn = list[i];
                strtmp += NameUtils.GoLangDialectID(NameUtils.LowercaseUnderlineToAllCapitalUpper(tableColumn.ColumnName)) + ": row." + NameUtils.GoLangDialectID(NameUtils.LowercaseUnderlineToAllCapitalUpper(tableColumn.ColumnName)) + "." + GolangTypeUtils.DbTypeNullToGo(GolangTypeUtils.DbTypeToGolangNull(tableColumn.ColumnType)) + "  , ";
                if ((i > 0) && (i % 3 == 0))
                {
                    strtmp += "\n";
                }
            }
            //整除会多个换行
            switch (list.Count % 3)
            {
                case 0:
                    strtmp = strtmp.Remove(strtmp.Length - 3);
                    break;
                case 1:
                    strtmp = strtmp.Remove(strtmp.Length - 6);
                    break;
                case 2:
                    strtmp = strtmp.Remove(strtmp.Length - 2);
                    break;
                case 3:
                    strtmp = strtmp.Remove(strtmp.Length - 2);
                    break;
            }

            stringBuilder.AppendLine(strtmp + "}");
            stringBuilder.AppendLine("return");
            stringBuilder.AppendLine("}");
            stringBuilder.AppendLine();
            return stringBuilder.ToString();
        }

        public static string GolangGetRow(string connectiongString, string dbName, string tableName)
        {
            List<TableColumnEntity> list = SchemaUtils.TableColumnToEntity(connectiongString, dbName, tableName);
            TableColumnEntity tableEntity = SchemaUtils.TableToEntity(connectiongString, dbName, tableName);
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.AppendLine();
            stringBuilder.AppendLine("// GetRow" + NameUtils.LowercaseUnderlineToAllCapitalUpper(tableName) + " string " + tableEntity.ColumnComment + " ORM To Entity List");
            stringBuilder.AppendLine("func GetRow"+ NameUtils.LowercaseUnderlineToAllCapitalUpper(tableName) + "(sqlText string, params ...interface{})" + "(rowResult *" + NameUtils.LowercaseUnderlineToAllCapitalUpper(tableName) + ", err error){");
            stringBuilder.AppendLine("query := MysqlDb.QueryRow(sqlText, params...)");
            stringBuilder.AppendLine("rowResult,err = RowToObject"+ NameUtils.LowercaseUnderlineToAllCapitalUpper(tableName) + "(query)");
            stringBuilder.AppendLine("if err != nil && err != sql.ErrNoRows {");
            stringBuilder.AppendLine("utils.Debug(\"GetRow" + NameUtils.LowercaseUnderlineToAllCapitalUpper(tableName) + " QueryToObject\", sqlText, params, err)");
            stringBuilder.AppendLine("  return");
            stringBuilder.AppendLine("}");
            stringBuilder.AppendLine("return");
            stringBuilder.AppendLine("}");
            stringBuilder.AppendLine();
            return stringBuilder.ToString();
        }
        public static string GolangGetRows(string connectiongString, string dbName, string tableName)
        {
            List<TableColumnEntity> list = SchemaUtils.TableColumnToEntity(connectiongString, dbName, tableName);
            TableColumnEntity tableEntity = SchemaUtils.TableToEntity(connectiongString, dbName, tableName);
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.AppendLine();
            stringBuilder.AppendLine("// GetRows" + NameUtils.LowercaseUnderlineToAllCapitalUpper(tableName) + " string " + tableEntity.ColumnComment + " ORM To Entity List");
            stringBuilder.AppendLine("func GetRows"+ NameUtils.LowercaseUnderlineToAllCapitalUpper(tableName) + "(sqlText string, params ...interface{})" + "(rowsResult []*" + NameUtils.LowercaseUnderlineToAllCapitalUpper(tableName) + ", err error){");
            stringBuilder.AppendLine("query, err := MysqlDb.Query(sqlText, params...)");
            stringBuilder.AppendLine("if err != nil {");
            stringBuilder.AppendLine("utils.Debug(\"GetRow" + NameUtils.LowercaseUnderlineToAllCapitalUpper(tableName) + " Query\", sqlText, params, err)");
            stringBuilder.AppendLine("return");
            stringBuilder.AppendLine("}");
            stringBuilder.AppendLine("defer query.Close()");
            stringBuilder.AppendLine("for query.Next() {");
            stringBuilder.AppendLine("rowResult,err := RowsToObject"+ NameUtils.LowercaseUnderlineToAllCapitalUpper(tableName) + "(query)");
            stringBuilder.AppendLine("if err != nil && err != sql.ErrNoRows {");
            stringBuilder.AppendLine("utils.Debug(\"GetRows" + NameUtils.LowercaseUnderlineToAllCapitalUpper(tableName) + " QueryToObject\", sqlText, params, err)");
            stringBuilder.AppendLine("  return nil, nil");
            stringBuilder.AppendLine("}");
            stringBuilder.AppendLine("rowsResult = append(rowsResult, rowResult)");
            stringBuilder.AppendLine("}");
            stringBuilder.AppendLine("return");
            stringBuilder.AppendLine("}");
            stringBuilder.AppendLine();
            return stringBuilder.ToString();
        }

        public static string GolangUpdateOrDelete(string connectiongString, string dbName, string tableName)
        {
            List<TableColumnEntity> list = SchemaUtils.TableColumnToEntity(connectiongString, dbName, tableName);
            TableColumnEntity tableEntity = SchemaUtils.TableToEntity(connectiongString, dbName, tableName);
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.AppendLine();
            stringBuilder.AppendLine("// UpdateOrDelete" + NameUtils.LowercaseUnderlineToAllCapitalUpper(tableName) + " int64, err " + tableEntity.ColumnComment + " Update or Delete Sql Segment");
            stringBuilder.AppendLine("func UpdateOrDelete"+ NameUtils.LowercaseUnderlineToAllCapitalUpper(tableName) + "(sqlText string, params ...interface{})" + "(affectCount int64, err error){");
            stringBuilder.AppendLine("stmt, err := MysqlDb.Prepare(sqlText)");
            stringBuilder.AppendLine("if err != nil {");
            stringBuilder.AppendLine("utils.Debug(\"UpdateOrDelete" + NameUtils.LowercaseUnderlineToAllCapitalUpper(tableName) + " Prepare\", sqlText, params, err)");
            stringBuilder.AppendLine("return");
            stringBuilder.AppendLine("}");
            stringBuilder.AppendLine("defer stmt.Close()");
            stringBuilder.AppendLine("result, err := stmt.Exec(params...)");
            stringBuilder.AppendLine("if err != nil {");
            stringBuilder.AppendLine("utils.Debug(\"UpdateOrDelete" + NameUtils.LowercaseUnderlineToAllCapitalUpper(tableName) + " Exec\", sqlText, params, err)");
            stringBuilder.AppendLine("return");
            stringBuilder.AppendLine("}");
            stringBuilder.AppendLine("affectCount, err = result.RowsAffected()");
            stringBuilder.AppendLine("if err != nil {");
            stringBuilder.AppendLine("utils.Debug(\"UpdateOrDelete" + NameUtils.LowercaseUnderlineToAllCapitalUpper(tableName) + " RowsAffected\", sqlText, params, err)");
            stringBuilder.AppendLine("return");
            stringBuilder.AppendLine("}");
            stringBuilder.AppendLine("return");
            stringBuilder.AppendLine("}");
            return stringBuilder.ToString();
        }

        public static string GolangUpdateSQLAndParamsSegment(string connectiongString, string dbName, string tableName)
        {
            List<TableColumnEntity> list = SchemaUtils.TableColumnToEntity(connectiongString, dbName, tableName);
            TableColumnEntity tableEntity = SchemaUtils.TableToEntity(connectiongString, dbName, tableName);
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.AppendLine();
            stringBuilder.AppendLine("// GolangUpdateSQLAndParamsSegment"+ NameUtils.LowercaseUnderlineToAllCapitalUpper(tableName) +" " + tableEntity.ColumnComment + " Update");
            stringBuilder.AppendLine("func GolangUpdateSQLAndParamsSegment"+ NameUtils.LowercaseUnderlineToAllCapitalUpper(tableName) + "(){");
            string strtmp = string.Empty;
            for (int i = 0; i < list.Count; i++)
            {
                TableColumnEntity tableColumn = list[i];
                strtmp += "`" + tableColumn.ColumnName + "`=?  , ";
                if ((i > 0) && (i % 3 == 0))
                {
                    strtmp = strtmp+ "\"+\n\"";
                }
            }
            //整除会多个换行
            switch (list.Count % 3)
            {
                case 0:
                    strtmp = strtmp.Remove(strtmp.Length - 3);
                    break;
                case 1:
                    strtmp = strtmp.Remove(strtmp.Length - 6);
                    break;
                case 2:
                    strtmp = strtmp.Remove(strtmp.Length - 2);
                    break;
                case 3:
                    strtmp = strtmp.Remove(strtmp.Length - 2);
                    break;
            }

            stringBuilder.AppendLine("sqlText:=\""+strtmp+"\"");

            strtmp = string.Empty;
            for (int i = 0; i < list.Count; i++)
            {
                TableColumnEntity tableColumn = list[i];
                strtmp += "&"+NameUtils.LowercaseUnderlineToCammle(tableName) +"."+ NameUtils.GoLangDialectID(NameUtils.LowercaseUnderlineToAllCapitalUpper(tableColumn.ColumnName)) + "  ,  ";
                if ((i > 0) && (i % 3 == 0))
                {
                    strtmp = strtmp + "\n";
                }
            }
            //整除会多个换行
            switch (list.Count % 3)
            {
                case 0:
                    strtmp = strtmp.Remove(strtmp.Length - 3);
                    break;
                case 1:
                    strtmp = strtmp.Remove(strtmp.Length - 6);
                    break;
                case 2:
                    strtmp = strtmp.Remove(strtmp.Length - 2);
                    break;
                case 3:
                    strtmp = strtmp.Remove(strtmp.Length - 2);
                    break;
            }
            stringBuilder.AppendLine("paramText:=`" + strtmp + "`");
            stringBuilder.AppendLine("utils.Debug(sqlText, paramText)");
            stringBuilder.AppendLine("}");
            return stringBuilder.ToString();
        }

        public static string GolangInsert(string connectiongString, string dbName, string tableName)
        {
            List<TableColumnEntity> list = SchemaUtils.TableColumnToEntity(connectiongString, dbName, tableName);
            TableColumnEntity tableEntity = SchemaUtils.TableToEntity(connectiongString, dbName, tableName);
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.AppendLine();
            stringBuilder.AppendLine("// Insert" + NameUtils.LowercaseUnderlineToAllCapitalUpper(tableName) + " int64, err " + tableEntity.ColumnComment + "  Insert New Recorder");
            stringBuilder.AppendLine("func Insert" + NameUtils.LowercaseUnderlineToAllCapitalUpper(tableName) + "(value *" + NameUtils.LowercaseUnderlineToAllCapitalUpper(tableName) + ") (lastID int64, err error) {");
            string strtmp = string.Empty;
            for (int i = 0; i < list.Count; i++)
            {
                TableColumnEntity tableColumn = list[i];
                strtmp += "`" + tableColumn.ColumnName + "`  , ";
                if ((i > 0) && (i % 3 == 0))
                {
                    strtmp = strtmp + "\"+\n\"";
                }
            }
            //整除会多个换行
            switch (list.Count % 3)
            {
                case 0:
                    strtmp = strtmp.Remove(strtmp.Length - 3);
                    break;
                case 1:
                    strtmp = strtmp.Remove(strtmp.Length - 6);
                    break;
                case 2:
                    strtmp = strtmp.Remove(strtmp.Length - 2);
                    break;
                case 3:
                    strtmp = strtmp.Remove(strtmp.Length - 2);
                    break;
            }


            stringBuilder.AppendLine("sqlTextField:=\"" + strtmp + "\"");

            strtmp = string.Empty;
            for (int i = 0; i < list.Count; i++)
            {
                TableColumnEntity tableColumn = list[i];
                strtmp += "? , ";
                if ((i > 0) && (i % 3 == 0))
                {
                    strtmp = strtmp + "\"+\n\"";
                }
            }
            //整除会多个换行
            switch (list.Count % 3) {
                case 0:
                    strtmp = strtmp.Remove(strtmp.Length - 3);
                    break;
                case 1:
                    strtmp = strtmp.Remove(strtmp.Length - 6);
                    break;
                case 2:
                    strtmp = strtmp.Remove(strtmp.Length - 2);
                    break;
                case 3:
                    strtmp = strtmp.Remove(strtmp.Length - 2);
                    break;
            }
            

            stringBuilder.AppendLine("sqlTextValue:=\"" + strtmp + "\"");

            stringBuilder.AppendLine("sqlText := \"INSERT INTO " + tableName + "(\"+sqlTextField+\")\"+");
            stringBuilder.AppendLine("\"VALUES(\" + sqlTextValue + \")\"");
            stringBuilder.AppendLine("stmt, err := MysqlDb.Prepare(sqlText)");
            stringBuilder.AppendLine("if err != nil {");
            stringBuilder.AppendLine("utils.Debug(\"Insert" + NameUtils.LowercaseUnderlineToAllCapitalUpper(tableName) + " Prepare\", sqlText, err)");
            stringBuilder.AppendLine("return");
            stringBuilder.AppendLine("}");
            stringBuilder.AppendLine("defer stmt.Close()");
            stringBuilder.AppendLine("value.CreateTime = utils.GetLocalTimeString(time.Now())");
            stringBuilder.AppendLine("value.LastUpdateTime = utils.GetLocalTimeString(time.Now())");
            stringBuilder.AppendLine("value.OperatorID = 1");
            stringBuilder.AppendLine("value.BeActive = true");
            stringBuilder.AppendLine("value.SysRowEditable = true");
            stringBuilder.AppendLine("value.SysRowDelete = true");
            stringBuilder.AppendLine("value.Version = 1");
            stringBuilder.AppendLine("result, err := stmt.Exec(");
            strtmp = string.Empty;
            for (int i = 0; i < list.Count; i++)
            {
                TableColumnEntity tableColumn = list[i];
                strtmp += "&value." + NameUtils.GoLangDialectID(NameUtils.LowercaseUnderlineToAllCapitalUpper(tableColumn.ColumnName)) + "  , ";
                if ((i > 0) && (i % 3 == 0))
                {
                    strtmp += "\n";
                }
            }
            //整除会多个换行
            strtmp = (list.Count % 3 > 1) ? strtmp.Remove(strtmp.Length - 2) : strtmp.Remove(strtmp.Length - 4);

            stringBuilder.AppendLine(strtmp + ")");
            stringBuilder.AppendLine("if err != nil {");
            stringBuilder.AppendLine("utils.Debug(\"Insert" + NameUtils.LowercaseUnderlineToAllCapitalUpper(tableName) + " Exec\", sqlText, err)");
            stringBuilder.AppendLine("return");
            stringBuilder.AppendLine("}");
            stringBuilder.AppendLine("lastID, err = result.LastInsertId()");
            stringBuilder.AppendLine("if err != nil {");
            stringBuilder.AppendLine("utils.Debug(\"Insert" + NameUtils.LowercaseUnderlineToAllCapitalUpper(tableName) + " LastInsertId\", sqlText, err)");
            stringBuilder.AppendLine("return");
            stringBuilder.AppendLine("}");
            stringBuilder.AppendLine("return");
            stringBuilder.AppendLine("}");

            return stringBuilder.ToString();
        }
    }
}